//
//  AMapPOISearchResponse+LocationModel.m
//  Anda_Driver_Pro
//
//  Created by AnDa-Y on 2019/8/2.
//  Copyright © 2019 AnDa-Y. All rights reserved.
//

#import "AMapPOISearchResponse+LocationModel.h"
#import "ADLocationModel.h"

@implementation AMapPOISearchResponse (LocationModel)
- (NSArray<ADLocationModel *> *)locationModels
{
    NSMutableArray *locations = [NSMutableArray array];
    [self.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop){
        ADLocationModel *location = [[ADLocationModel alloc] init];
        location.name = obj.name;
        location.latitude = obj.location.latitude;
        location.longitude = obj.location.longitude;
        location.address = [NSString stringWithFormat:@"%@%@%@",obj.city,obj.district,obj.address];
        location.city = obj.city;
        location.province = obj.province;
        [locations addObject:location];
        
        NSMutableArray *subLocations = [NSMutableArray arrayWithCapacity:obj.subPOIs.count];
        [obj.subPOIs enumerateObjectsUsingBlock:^(AMapSubPOI * _Nonnull sObj, NSUInteger idx, BOOL * _Nonnull stop) {
            ADLocationModel *subLocation = [[ADLocationModel alloc] init];
            subLocation.name = sObj.sname;
            subLocation.latitude = sObj.location.latitude;
            subLocation.longitude = sObj.location.longitude;
            subLocation.address = [NSString stringWithFormat:@"%@%@%@",obj.city,obj.district,obj.address];
            subLocation.city = obj.city;
            subLocation.province = obj.province;
            subLocation.adcode = obj.adcode;
            [subLocations addObject:subLocation];
        }];
        [subLocations sortUsingComparator:^NSComparisonResult(AMapSubPOI * _Nonnull obj1,AMapSubPOI * _Nonnull obj2) {
            return obj1.distance > obj2.distance;
        }];
        location.subLocations = subLocations;
    
    }];
    return locations;
}
@end
